package com.usts2022.classcostrecord.entity;


import android.app.Application;
import android.os.Bundle;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.lifecycle.AndroidViewModel;

import com.alibaba.fastjson.JSON;
import com.usts2022.classcostrecord.Utils.HttpUtils;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class UserViewModel extends AndroidViewModel {
    private User user;
    private List<Report> reports;

    public List<Report> getReports() {
        return reports;
    }

    public void setReports(List<Report> reports) {
        this.reports = reports;
    }

    public User getUser() {
        return user;
    }

    public void setUser(User user) {
        this.user = user;
    }

    public UserViewModel(@NonNull Application application) throws IOException {
        super(application);
    }

    //检验登录信息
    public void isLogin(Application application,String account,String password) throws IOException {
        String url = "http://192.168.10.1:8080/user/login/" + account + "/" + password;
        HttpUtils.get(url, new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                Looper.prepare();
                Looper.loop();
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                Looper.prepare();
                String str = response.body().string();
                if (response.code() != 200 || str.equals("")) {
                    Toast.makeText(application, "请登录", Toast.LENGTH_SHORT).show();
                    String string = response.body().string();
                    user = JSON.parseObject(string, User.class);
                    return;
                }
                Looper.loop();
            }
        });
    }

    /**
     *  从文件中读取数据
     * @return 读取到的数据
     */
    private String[] dataLoad(Application application) {
        FileInputStream in = null;
        BufferedReader reader = null;
        StringBuilder builder = new StringBuilder();
        try {
            in = application.openFileInput("loginData");
            reader = new BufferedReader(new InputStreamReader(in));
            String line = "";
            while ((line = reader.readLine()) != null) {
                builder.append(line);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        String[] dataList = builder.toString().split(" ");
        return dataList;
    }
}
